package qq.android.wan.qq.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import qq.android.wan.qq.R;

/**
 * 原理:两张图片相交
 * @author Administrator
 *
 */
public class CircleImageView extends ImageView {
	private static final Xfermode MASK_XFERMODE; // 设置两张图片相交时的模式
	private Bitmap mask;
	private Paint paint;
	private int mBorderWidth = 10;
	private int mBorderColor = Color.parseColor("#f2f2f2");

	static {
		PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;
		// 图层模式请看http://blog.csdn.net/edisonlg/article/details/7084977
		MASK_XFERMODE = new PorterDuffXfermode(localMode);
	}

	public CircleImageView(Context paramContext) {
		super(paramContext);
	}

	public CircleImageView(Context paramContext, AttributeSet paramAttributeSet) {
		this(paramContext, paramAttributeSet, 0);
	}

	public CircleImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
		super(paramContext, paramAttributeSet, paramInt);
		this.setClickable(true);
		TypedArray a = paramContext.obtainStyledAttributes(paramAttributeSet, R.styleable.CircularImage);
		mBorderColor = a.getColor(R.styleable.CircularImage_border_color, mBorderColor);
		//final int defalut = (int) (2 * paramContext.getResources().getDisplayMetrics().density + 0.5f);
		mBorderWidth = a.getDimensionPixelOffset(R.styleable.CircularImage_border_width, 0);
		a.recycle();
	}


	@Override
	protected void onDraw(Canvas paramCanvas) {

		//super.onDraw(paramCanvas);
		// 得到我们在imageview中设置的图片
		Drawable localDrawable = getDrawable();// 当在imageview中设置src属性后，getDrawable()就不为null
		if (localDrawable == null) {
			return;
		}
		if (localDrawable instanceof NinePatchDrawable) {
			return;
		}
		int height=getHeight();
		int width=getWidth();//得到imageview的宽度
		//设置源图片的画笔
		Paint localPaint=new Paint();
		int layer = paramCanvas.saveLayer(0.0F, 0.0F, width, height, null, 31);
		localPaint.setFilterBitmap(true);//对位图进行滤波处理
		localPaint.setAntiAlias(false);//防止图片边缘的锯齿
		localPaint.setXfermode(MASK_XFERMODE);//设置两张图片重合时的模式
		localDrawable.setBounds(0,0, width, height);
		//这个函数可以将Drawable对象画在画布(屏幕)上
		localDrawable.draw(paramCanvas);
		//圆形的bitmap对象
		mask=createOvalBitmap(width, height);
		paramCanvas.drawBitmap(mask, 0,0, localPaint);
		paramCanvas.restoreToCount(layer);
		drawBorder(paramCanvas, width, height);
	}

	/**
	 * 绘制�?外面的边�?
	 *
	 * @param canvas
	 * @param width
	 * @param height
	 */
	private void drawBorder(Canvas canvas, final int width, final int height) {
		if (mBorderWidth == 0) {
			return;
		}
		final Paint mBorderPaint = new Paint();
		mBorderPaint.setStyle(Paint.Style.STROKE);
		mBorderPaint.setAntiAlias(true);
		mBorderPaint.setColor(mBorderColor);
		mBorderPaint.setStrokeWidth(mBorderWidth);
		/**
		 * 坐标x：view宽度的一�? 坐标y：view高度的一�? 半径r：因为是view的宽�?-border的一�?
		 */
		canvas.drawCircle(width >> 1, height >> 1, (width - mBorderWidth) >> 1, mBorderPaint);
		canvas = null;
	}

	/**
	 * 获取�?个bitmap，目的是用来承载drawable;
	 * <p>
	 * 将这个bitmap放在canvas上面承载，并在其上面画一个椭�?(其实也是�?个圆，因为width=height)来固定显示区�?
	 *
	 * @param width
	 * @param height
	 * @return
	 */
	public Bitmap createOvalBitmap(final int width, final int height) {
		Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;
		Bitmap localBitmap = Bitmap.createBitmap(width, height, localConfig);
		Canvas localCanvas = new Canvas(localBitmap);
		Paint localPaint = new Paint();
		localPaint.setAntiAlias(true);
		localPaint.setColor(Color.YELLOW);
		RectF localRectF = new RectF(0,0, width , height);
		localCanvas.drawOval(localRectF, localPaint);
		return localBitmap;
	}
}
